package www_urban

import (
	"fmt"
	"gitee.com/theegg/go-brick/app/model/system/system_districts"
	"gitee.com/theegg/go-brick/app/model/urban/urban_elements"
	"gitee.com/theegg/go-brick/app/service/app"
	"gitee.com/theegg/go-brick/app/service/help/cbcrypt"
	"gitee.com/theegg/go-brick/app/service/help/configure"
	"gitee.com/theegg/go-brick/app/service/help/cstruct"
	"github.com/gin-gonic/gin"
	"strconv"
)

var country string = "cn"

// @title GetTourList
// @description 获取旅游景点
func GetTourList(c *gin.Context) {
	mainType := 1
	// 检验参数
	var validaParams urban_elements.QueryParams
	if err := c.ShouldBind(&validaParams); err != nil {
		app.NewResponse(c).JsonReturn(false, err.Error(), nil)
		return
	}

	// 指定国家
	if validaParams.Country == "" {
		valCountry, err := configure.Value("urban_country", "cn")
		fmt.Println(country)
		if err != nil {
			app.NewResponse(c).JsonReturn(false, err.Error(), nil)
			return
		}

		validaParams.Country = valCountry.(string)
		country = valCountry.(string)
	}

	validaParams.TypeId = mainType
	data, err := urban_elements.GetDataList(cstruct.StructToMap(validaParams))
	if err != nil {
		app.NewResponse(c).JsonReturn(false, err.Error(), data)
		return
	}

	tmpData := make([]map[string]string, 0)
	for _, v := range data["data"].([]urban_elements.UrbanElements) {
		tmpData = append(tmpData, map[string]string{
			"image_cover_url": app.GetImageStaticUrl(v.ImageCover),
			"title_cn":        v.Title,
			"title_en":        v.TitleEn,
			"id":              strconv.FormatInt(v.Id, 10),
		})
	}

	data["data"] = tmpData

	typeOptions, errType := urban_elements.GetElementTypeSortOptions(mainType)
	if errType != nil {
		app.NewResponse(c).JsonReturn(false, errType.Error(), data)
		return
	}

	data["type_options"] = typeOptions
	districtData, errDistrict := system_districts.GetDistrictSortOptionByCountry(country)
	if errDistrict != nil {
		app.NewResponse(c).JsonReturn(false, errDistrict.Error(), data)
		return
	}

	data["district_options"] = districtData

	app.NewResponse(c).JsonReturn(true, "获取数据成功", data)
	return
}

func GetTourDetail(c *gin.Context) {
	idStr := c.PostForm("id")
	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		app.NewResponse(c).JsonReturn(false, err.Error(), nil)
		return
	}

	item, errGet := urban_elements.GetItemById(id)
	if errGet != nil {
		app.NewResponse(c).JsonReturn(false, errGet.Error(), nil)
		return
	}

	aesKey := app.GetAesKey()

	data := map[string]interface{}{
		"image_cover_url": app.GetImageStaticUrl(item.ImageCover),
		"province_name":   cbcrypt.AesEncrypt([]byte(system_districts.GetDistrictName(item.ProvinceId)), aesKey),
		"city_name":       cbcrypt.AesEncrypt([]byte(system_districts.GetDistrictName(item.CityId)), aesKey),
		"cn_name":         cbcrypt.AesEncrypt([]byte(item.Title), aesKey),
		"en_name":         cbcrypt.AesEncrypt([]byte(item.TitleEn), aesKey),
		"address":         cbcrypt.AesEncrypt([]byte(item.Address), aesKey),
		"open_time":       item.OpenTime,
		"close_time":      item.CloseTime,
		"fee_ticket_has":  item.FeeTicketHas,
		"fee_amount":      item.FeeAmount,
		"summary_cn":      item.Summary,
		"coordinate_x":    cbcrypt.AesEncrypt([]byte(item.CoordinateX), aesKey),
		"coordinate_y":    cbcrypt.AesEncrypt([]byte(item.CoordinateY), aesKey),
	}

	app.NewResponse(c).JsonReturn(true, "获取数据成功", data)
	return
}
